package com.alex.database;

import java.util.Vector;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.alex.books.BaseBook;
import com.alex.books.BookMark;
import com.alex.books.Chapter;
import com.alex.database.BookDatabaseHelper.Book_TB;
import com.alex.database.BookDatabaseHelper.Bookmark_TB;
import com.alex.database.BookDatabaseHelper.Chapter_TB;

/**
 * 
 * 书本操作
 * 
 * @author Alex.Lu
 *
 */
public class BookDao {
	
	
	private static BookDao instance;
	
	public static BookDao getInstance(){
		if(instance == null){
			instance = new BookDao();
		}
		return instance;
	}
	
	
	/**
	 * 插入书本
	 * @param context
	 * @param bookmark
	 */
	public long insertBook(Context context,BaseBook book){
		long id;
		SQLiteDatabase db = BookDatabaseHelper.getInstance(context).getWritableDatabase();
		db.beginTransaction();
		ContentValues values = new ContentValues();
		values.put(Book_TB.BOOK_NAME, book.mBookName);
		values.put(Book_TB.BOOK_SERIAL, book.mBookSerial);
		values.put(Book_TB.BOOK_PATH, book.mBookPath);
		values.put(Book_TB.BOOK_AUTHOER, book.mBookAuthor);
		values.put(Book_TB.BOOK_WORDNUMBER, book.mWordNumber);
		values.put(Book_TB.BOOK_CHAPTERNUMBER, book.mChapterNumber);
		values.put(Book_TB.BOOK_SOURCE, book.mSource);
		values.put(Book_TB.BOOK_ABOUTAUTHOR, book.mAboutAuthor);
		values.put(Book_TB.BOOK_ABOUTBOOK, book.mAboutBook);
		values.put(Book_TB.BOOK_COVERURL, book.mBookCover);
		values.put(Book_TB.BOOK_ATTACH1, book.mAttach1);
		values.put(Book_TB.BOOK_ATTACH2, book.mAttach2);
		values.put(Book_TB.BOOk_LASTTIME, book.mLastTime);
		values.put(Book_TB.BOOK_MARK, book.mReaderingInfo.mMarkPos);
		values.put(Book_TB.BOOK_ISFIRSTREAD, 1);
		values.put(Book_TB.BOOK_CHAPTER_INDEX, book.mReaderingInfo.mIndex);
		values.put(Book_TB.CHAPTER_ID, book.mReaderingInfo.mCurChapter.id);
		values.put(Book_TB.CHAPTER_INDEX, book.mReaderingInfo.mIndex);
		values.put(Book_TB.CHAPTER_NAME, book.mReaderingInfo.mCurChapter.mChapterName);
		values.put(Book_TB.CHAPTER_BEGIN, book.mReaderingInfo.mCurChapter.mPosBegin);
		values.put(Book_TB.CHAPTER_END, book.mReaderingInfo.mCurChapter.mPosEnd);
		values.put(Book_TB.CHAPTER_LENGTH, book.mReaderingInfo.mCurChapter.mLength);
		id = db.insert(Book_TB.TABLE_NAME, null, values);
		db.setTransactionSuccessful();
		db.endTransaction();
		return id;
	}
	
	/**
	 * 书本
	 * @param context
	 * @return
	 */
	public Cursor queryBook(Context context,int book_id){
		String[] projection = {
				Book_TB._ID,
				Book_TB.BOOK_NAME,
				Book_TB.BOOK_SERIAL,
				Book_TB.BOOK_MARK,
				Book_TB.BOOK_PATH,
				Book_TB.BOOK_WORDNUMBER,
				Book_TB.BOOK_CHAPTERNUMBER,
				Book_TB.BOOK_ABOUTAUTHOR,
				Book_TB.BOOK_ABOUTBOOK,
				Book_TB.BOOK_AUTHOER,
				Book_TB.BOOK_SOURCE,
				Book_TB.BOOK_COVERURL,
				Book_TB.BOOK_ATTACH1,
				Book_TB.BOOK_ATTACH2,
				Book_TB.BOOk_LASTTIME,
				Book_TB.BOOK_MARKSNUMBER,
				Book_TB.BOOK_ISFIRSTREAD,
				Book_TB.BOOK_CHAPTER_INDEX,
				Book_TB.CHAPTER_ID,
				Book_TB.CHAPTER_INDEX,
				Book_TB.CHAPTER_NAME,
				Book_TB.CHAPTER_BEGIN,
				Book_TB.CHAPTER_END,
				Book_TB.CHAPTER_LENGTH
			};
		
		StringBuffer selection = new StringBuffer();
			selection.append(Book_TB._ID);
			selection.append("=");
			selection.append(book_id);
		
		SQLiteDatabase db = BookDatabaseHelper.getInstance(context).getReadableDatabase();
		Cursor c = db.query(Book_TB.TABLE_NAME, projection, selection.toString(), null, null, null, null);
		return c;
	}
	
	
	public void updateBook(Context context,BaseBook book){
		SQLiteDatabase db = BookDatabaseHelper.getInstance(context).getWritableDatabase();
		db.beginTransaction();
		ContentValues values = new ContentValues();
		values.put(Book_TB.BOOK_NAME, book.mBookName);
		values.put(Book_TB.BOOK_SERIAL, book.mBookSerial);
		values.put(Book_TB.BOOK_PATH, book.mBookPath);
		values.put(Book_TB.BOOK_AUTHOER, book.mBookAuthor);
		values.put(Book_TB.BOOK_WORDNUMBER, book.mWordNumber);
		values.put(Book_TB.BOOK_CHAPTERNUMBER, book.mChapterNumber);
		values.put(Book_TB.BOOK_SOURCE, book.mSource);
		values.put(Book_TB.BOOK_ABOUTAUTHOR, book.mAboutAuthor);
		values.put(Book_TB.BOOK_ABOUTBOOK, book.mAboutBook);
		values.put(Book_TB.BOOK_COVERURL, book.mBookCover);
		values.put(Book_TB.BOOK_ATTACH1, book.mAttach1);
		values.put(Book_TB.BOOK_ATTACH2, book.mAttach2);
		values.put(Book_TB.BOOk_LASTTIME, book.mLastTime);
		values.put(Book_TB.BOOK_MARKSNUMBER, book.mBookMarksNumber);
		values.put(Book_TB.BOOK_MARK, book.mReaderingInfo.mMarkPos);
		values.put(Book_TB.BOOK_ISFIRSTREAD, book.mIsFirstReader);
		values.put(Book_TB.BOOK_CHAPTER_INDEX, book.mReaderingInfo.mIndex);
		values.put(Book_TB.CHAPTER_ID, book.mReaderingInfo.mCurChapter.id);
		values.put(Book_TB.CHAPTER_INDEX, book.mReaderingInfo.mCurChapter.mChapterIndex);
		values.put(Book_TB.CHAPTER_NAME, book.mReaderingInfo.mCurChapter.mChapterName);
		values.put(Book_TB.CHAPTER_BEGIN, book.mReaderingInfo.mCurChapter.mPosBegin);
		values.put(Book_TB.CHAPTER_END, book.mReaderingInfo.mCurChapter.mPosEnd);
		values.put(Book_TB.CHAPTER_LENGTH, book.mReaderingInfo.mCurChapter.mLength);
		StringBuffer whereClause = new StringBuffer();
		whereClause.append(Book_TB._ID);
		whereClause.append(" = ");
		whereClause.append(book.mBookId);
		db.update(Book_TB.TABLE_NAME, values, whereClause.toString(), null);
		db.setTransactionSuccessful();
		db.endTransaction();
	}
	
	/**
	 * 获取书本列表
	 * @param context
	 * @return
	 */
	public Cursor queryBooks(Context context,String chars,String orderBy){
		String[] projection = {
				Book_TB._ID,
				Book_TB.BOOK_NAME,
				Book_TB.BOOK_SERIAL,
				Book_TB.BOOK_PATH,
				Book_TB.BOOK_AUTHOER,
				Book_TB.BOOK_WORDNUMBER,
				Book_TB.BOOK_CHAPTERNUMBER,
				Book_TB.BOOK_SOURCE,
				Book_TB.BOOK_ABOUTAUTHOR,
				Book_TB.BOOK_ABOUTBOOK,
				Book_TB.BOOK_COVERURL,
				Book_TB.BOOK_MARK,
				Book_TB.BOOK_ATTACH1,
				Book_TB.BOOK_ATTACH2,
				Book_TB.BOOk_LASTTIME,
				Book_TB.BOOK_MARKSNUMBER,
				Book_TB.BOOK_ISFIRSTREAD,
				Book_TB.BOOK_CHAPTER_INDEX,
				Book_TB.CHAPTER_ID,
				Book_TB.CHAPTER_INDEX,
				Book_TB.CHAPTER_NAME,
				Book_TB.CHAPTER_BEGIN,
				Book_TB.CHAPTER_END,
				Book_TB.CHAPTER_LENGTH
			};
		StringBuffer selection = new StringBuffer();
		if(chars!=null){
			selection.append(Book_TB.BOOK_NAME);
			selection.append(" like '%");
			selection.append(chars);
			selection.append("%'");
		}
		SQLiteDatabase db = BookDatabaseHelper.getInstance(context).getReadableDatabase();
		Cursor c = db.query(Book_TB.TABLE_NAME, projection,selection.toString(), null, null, null, orderBy);
		return c;
	}
	
	
	public int deleteBook(Context context,BaseBook book){
		int relust;
		SQLiteDatabase db = BookDatabaseHelper.getInstance(context).getWritableDatabase();
		db.beginTransaction();
		StringBuffer whereClause = new StringBuffer();
		whereClause.append(Book_TB._ID);
		whereClause.append(" = ");
		whereClause.append(book.mBookId);
		relust = db.delete(Book_TB.TABLE_NAME, whereClause.toString(), null);
		db.setTransactionSuccessful();
		db.endTransaction();
		return relust;
	}
	
	
	/**
	 * 插入标签
	 * @param context
	 * @param bookmark
	 */
	public long insertBookmark(Context context,BookMark bookmark){
		long retuen_id;
		SQLiteDatabase db = BookDatabaseHelper.getInstance(context).getWritableDatabase();
		db.beginTransaction();
		ContentValues values = new ContentValues();
		values.put(Bookmark_TB.BOOK_ID, bookmark.mBookId);
		values.put(Bookmark_TB.BOOKMARK_NAME, bookmark.mName);
		values.put(Bookmark_TB.BOOKMARK_MARK, bookmark.mMarkPos);
		values.put(Bookmark_TB.BOOKMARK_INTRO, bookmark.mIntro);
		values.put(Bookmark_TB.BOOkMARK_TIME, bookmark.mTime);
		values.put(Bookmark_TB.CHAPTER_ID, bookmark.mChapter.id);
		values.put(Bookmark_TB.CHAPTER_INDEX, bookmark.mChapter.mChapterIndex);
		values.put(Bookmark_TB.CHAPTER_NAME, bookmark.mChapter.mChapterName);
		values.put(Bookmark_TB.CHAPTER_BEGIN, bookmark.mChapter.mPosBegin);
		values.put(Bookmark_TB.CHAPTER_END, bookmark.mChapter.mPosEnd);
		values.put(Bookmark_TB.CHAPTER_LENGTH, bookmark.mChapter.mLength);
		retuen_id = db.insert(Bookmark_TB.TABLE_NAME, null, values);
		db.setTransactionSuccessful();
		db.endTransaction();
		return retuen_id;
	}
	
	
	/**
	 * 书签
	 * @param context
	 * @return
	 */
	public Cursor queryBookMarks(Context context,int bookId){
		String[] projection = {
				Bookmark_TB._ID,
				Bookmark_TB.BOOK_ID,
				Bookmark_TB.BOOKMARK_NAME,
				Bookmark_TB.BOOKMARK_MARK,
				Bookmark_TB.BOOkMARK_TIME,
				Bookmark_TB.BOOKMARK_INTRO,
				Bookmark_TB.CHAPTER_ID,
				Bookmark_TB.CHAPTER_INDEX,
				Bookmark_TB.CHAPTER_NAME,
				Bookmark_TB.CHAPTER_BEGIN,
				Bookmark_TB.CHAPTER_END,
				Bookmark_TB.CHAPTER_LENGTH
			};
		StringBuffer whereClause = new StringBuffer();
		whereClause.append(Chapter_TB.BOOK_ID);
		whereClause.append(" = ");
		whereClause.append(bookId);
		SQLiteDatabase db = BookDatabaseHelper.getInstance(context).getReadableDatabase();
		Cursor c = db.query(Bookmark_TB.TABLE_NAME, projection, whereClause.toString(), null, null, null, null);
		return c;
	}
	
	
	
	/**
	 * 删除书签
	 * @param context
	 * @param bookMarkId 书签id
	 * @return
	 */
	public int deleteBookMarks(Context context,int bookMarkId){
		int relust;
		SQLiteDatabase db = BookDatabaseHelper.getInstance(context).getWritableDatabase();
		db.beginTransaction();
		StringBuffer whereClause = new StringBuffer();
		whereClause.append(Bookmark_TB._ID);
		whereClause.append(" = ");
		whereClause.append(bookMarkId);
		relust = db.delete(Bookmark_TB.TABLE_NAME, whereClause.toString(), null);
		db.setTransactionSuccessful();
		db.endTransaction();
		return relust;
	}
	
	/**
	 * 根据书本的id，删除该书的所有书签
	 * @param context
	 * @param bookId
	 * @return
	 */
	public int deleteBookMarksByBookId(Context context,int bookId){
		int relust;
		SQLiteDatabase db = BookDatabaseHelper.getInstance(context).getWritableDatabase();
		db.beginTransaction();
		StringBuffer whereClause = new StringBuffer();
		whereClause.append(Bookmark_TB.BOOK_ID);
		whereClause.append(" = ");
		whereClause.append(bookId);
		relust = db.delete(Bookmark_TB.TABLE_NAME, whereClause.toString(), null);
		db.setTransactionSuccessful();
		db.endTransaction();
		return relust;
	}
	
	/**
	 * 插入章节数据
	 * @param context 
	 * @param mChapter 章节
	 */
	public void insertChapter(Context context,Vector<Chapter> vec){
		SQLiteDatabase db = BookDatabaseHelper.getInstance(context).getWritableDatabase();
		db.beginTransaction();
		for(int i=0;i<vec.size();i++){
			Chapter chapter = (Chapter)vec.get(i);
			ContentValues values = new ContentValues();
			values.put(Chapter_TB.BOOK_ID, chapter.mBookId);
			values.put(Chapter_TB.CHAPTER_NAME, chapter.mChapterName);
			values.put(Chapter_TB.CHAPTER_INDEX, i);
			values.put(Chapter_TB.CHAPTER_BEGINPOS, chapter.mPosBegin);
			values.put(Chapter_TB.CHAPTER_ENDPOS, chapter.mPosEnd);
			values.put(Chapter_TB.CHAPTER_LENGTH, chapter.mLength);
			chapter.id = (int)db.insert(Chapter_TB.TABLE_NAME, null, values);
		}
		db.setTransactionSuccessful();
		db.endTransaction();
	}
	
	/**
	 * 
	 * 搜索章节数据
	 * 
	 * @param context
	 * @return
	 */
	public Cursor queryChapter(Context context,int bookId){
		String[] projection = {
				Chapter_TB._ID,
				Chapter_TB.BOOK_ID,
				Chapter_TB.CHAPTER_INDEX,
				Chapter_TB.CHAPTER_NAME,
				Chapter_TB.CHAPTER_BEGINPOS,
				Chapter_TB.CHAPTER_ENDPOS,
				Chapter_TB.CHAPTER_LENGTH
			};
		
		StringBuffer whereClause = new StringBuffer();
		whereClause.append(Chapter_TB.BOOK_ID);
		whereClause.append(" = ");
		whereClause.append(bookId);
		SQLiteDatabase db = BookDatabaseHelper.getInstance(context).getReadableDatabase();
		Cursor c = db.query(Chapter_TB.TABLE_NAME, projection, whereClause.toString(), null, null, null, null);
		return c;
	}
	
	
	/**
	 * 删除所有的书本的章节
	 * @param context
	 * @return
	 */
	public int deleteChapter(Context context,int book_id){
		int relust;
		SQLiteDatabase db = BookDatabaseHelper.getInstance(context).getWritableDatabase();
		db.beginTransaction();
		StringBuffer whereClause = new StringBuffer();
		whereClause.append(Chapter_TB.BOOK_ID);
		whereClause.append(" = ");
		whereClause.append(book_id);
		relust = db.delete(Chapter_TB.TABLE_NAME, whereClause.toString(), null);
		db.setTransactionSuccessful();
		db.endTransaction();
		return relust;
	}
}
